package org.mosquitos.chat;

import java.util.Map;

import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.peergroup.PeerGroup;

public class PeerAdvertisementDiscoveryService implements Runnable {
	private PeerGroup npg;
	private Map<String,Object> userList;
	
	public PeerAdvertisementDiscoveryService(PeerGroup npg,Map<String,Object> userList){
		this.npg = npg;
		this.userList = userList;
	}

	public void run() {
		DiscoveryService ds = npg.getDiscoveryService();
		long waittime = 1 * 2 * 1000L;
		
		while (true) {
			
			DiscoveryListener pal = new PeerAdvertisementListener(userList);
	        ds.addDiscoveryListener(pal);
	        ds.getRemoteAdvertisements(null, DiscoveryService.PEER,null,null,5,null);
	        
	        synchronized (this){
		        try {
					this.wait(waittime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
		}
	}
}
